package com.huluobo.a2202mvvm_im_douyindemo.shop;

import androidx.lifecycle.MutableLiveData;

import com.google.gson.Gson;
import com.huluobo.a2202mvvm_im_douyindemo.bean.AddToCartBeanResult;
import com.huluobo.a2202mvvm_im_douyindemo.bean.ShopBeanResult;
import com.huluobo.a2202mvvm_im_douyindemo.mvvmBase.BaseViewModel;
import com.huluobo.a2202mvvm_im_douyindemo.mvvmBase.net.Const;

import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by LC on 2024/5/14.
 */
public class ShopViewModel extends BaseViewModel<ShopRepo> {
    MutableLiveData<ShopBeanResult> getGoodsListSuccess = new MutableLiveData<>();

    MutableLiveData<String> getGoodsListFailed = new MutableLiveData<>();

    MutableLiveData<AddToCartBeanResult> addToCartSuccess = new MutableLiveData<>();
    MutableLiveData<String> addToCartFailed = new MutableLiveData<>();

    public void addToCart(int goodsId, int count) {
        Map<String, Integer> map = new HashMap<>();
        map.put(Const.PARAM_GOOD_ID, goodsId);
        map.put(Const.PARAM_COUNT, count);

        String json = new Gson().toJson(map);
        RequestBody body = RequestBody.create(MediaType.parse(Const.MEDIA_TYPE), json);
        repo.addToCart(body, addToCartSuccess, addToCartFailed);
    }

    public void getGoodsList(int categoryId, int currentPage, int pageSize) {
        repo.getGoodsList(categoryId, currentPage, pageSize, getGoodsListSuccess, getGoodsListFailed);
    }

    @Override
    protected ShopRepo createRepo() {
        return new ShopRepo();
    }
}
